package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
)

type SchoolValue struct {
	Id    int64  `json:"id"`
	Value string `json:"value"`
}

// 获取学校信息
func GetSchoolInfo(p map[string]interface{}) *models.School {
	o := orm.NewOrm()
	school := &models.School{}
	qs := o.QueryTable("School")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	qs.One(school)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if school.Id > 0 {
			school.StatusShow = models.SchoolStatus[school.Status]
		}
	}
	return school
}

// 添加学校
func AddSchool(input url.Values) (int64, error) {
	o := orm.NewOrm()
	school := &models.School{}
	province := &models.Region{}
	if _, ok := input["province_id"]; ok {
		province.Id = utils.Atoi64(input["province_id"][0])
		o.Read(province)
	}
	school.Province = province
	city := &models.Region{}
	if _, ok := input["city_id"]; ok {
		city.Id = utils.Atoi64(input["city_id"][0])
		o.Read(city)
	}
	school.City = city
	district := &models.Region{}
	if _, ok := input["district_id"]; ok {
		district.Id = utils.Atoi64(input["district_id"][0])
		o.Read(district)
	}
	school.District = district
	sn := utils.UniqueId()
	p := map[string]interface{}{}
	p["sn"] = sn
	schoolInfo := GetSchoolInfo(p)
	for schoolInfo.Id > 0 {
		sn = utils.UniqueId()
		p["sn"] = sn
		schoolInfo = GetSchoolInfo(p)
	}
	school.Sn = sn
	if _, ok := input["name"]; ok {
		school.Name = input["name"][0]
	}
	if _, ok := input["status"]; ok {
		school.Status, _ = strconv.Atoi(input["status"][0])
	}
	return o.Insert(school)
}

// 修改学校信息
func EditSchool(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("School").Filter("id", input["id"][0]).Update(p)
}

// 获取学校列表
func GetSchoolList(p map[string]interface{}) (int64, []*models.School) {
	var schoolList []*models.School
	o := orm.NewOrm()
	qs := o.QueryTable("School")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["provinceId"]; ok {
		qs = qs.Filter("province_id", p["provinceId"])
	}
	if _, ok := p["cityId"]; ok {
		qs = qs.Filter("city_id", p["cityId"])
	}
	if _, ok := p["districtId"]; ok {
		qs = qs.Filter("district_id", p["districtId"])
	}
	if _, ok := p["name"]; ok {
		qs = qs.Filter("name__contains", p["name"])
	}
	if _, ok := p["status"]; ok {
		qs = qs.Filter("status", p["status"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&schoolList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, school := range schoolList {
			school.StatusShow = models.SchoolStatus[school.Status]
		}
	}
	return totalCount, schoolList
}

func GetSchoolListValues(schoolList []*models.School) []*SchoolValue {
	data := []*SchoolValue{}
	for _, school := range schoolList {
		schoolVal := &SchoolValue{
			Id:    school.Id,
			Value: school.Name,
		}
		data = append(data, schoolVal)
	}
	return data
}